<?php
/**
 * 订单类
 * @author chenlisong E-mail:chenlisong1021@163.com
 * @version 创建时间：2016年10月24日 上午11:58:17
 * 类说明
 */
class OrderAction extends BaseAction{

	public function index(){
		echo $this->err("非法操作！");
	}


	/**
	 * 确认订单页
	 */
	public function goCreateOrder(){
		$Address = M('AddressSchool','v_');$Fields = M('Fields');$MailTime = M('MailTime');$Shop = M("Shop");
		$param = $this->param;
		//获取地址
		$addr = $Address->where('flag = 1 and is_main = 1 and user_id = '.$param['userId'])->field('id,school_id as schoolId,schoolName,province,city,area,address,floor_num,contact_tel as contactTel,contact_name as contactName')->find();
		$data['address'] = (object) $addr;
		$data['now'] = time();//暂时先放回昨天的时间
		//获取系统字段
		$data['mailSchoolStartFee'] = (float) $Fields->where('field = "app_mail_school_start_fee" and flag = 1')->getField('value');//校园模式起送费
		$data['mailCustomFee'] = (float) $Fields->where('field = "app_mail_custom_fee" and flag = 1')->getField('value');//快递模式配送费
		$data['mailSchoolFee'] = (float) $Fields->where('field = "app_mail_school_fee" and flag = 1')->getField('value');//校园模式配送费
		$data['mailFreeFee'] = (float) $Fields->where('field = "app_mail_free_fee" and flag = 1')->getField('value');//快递模式免配送费达到金额
		//获取配送时间段,校园模式含有
		$data['mailTimeList'] = $MailTime->where('flag = 1')->field('begin_time as beginTime,end_time as endTime')->select();
		//$data['mailTimeList'] = array(array('beginTime'=>date("H:i:s",time()),'endTime'=>date("H:i:s",time()+15*60)));
		if(isset($param['shop_id'])){
			$min_consumption = $Shop->where(array('id'=>$param['shop_id']))->getField('min_consumption');
			$data['min_consumption'] = $min_consumption;
		}else{
			$mailSchoolStartFee = (float) $Fields->where('field = "app_mail_school_start_fee" and flag = 1')->getField('value');//校园模式起送费
			$data['min_consumption'] = $mailSchoolStartFee;
		}
		echo $this->suc($data);
	}

	/**
	 * 创建订单
	 */
	public function createOrder(){
		$Order = M('Order');$OrderDetails = M('OrderDetails');$Address = M('AddressSchool','v_');$User = M('User');
		$Product = M('Product');$Fields = M('Fields');$Shop = M("Shop");$ShopProduct = M("ShopProduct");$ShopAttributeValues = M("ShopAttributeValues");
		$shop_is_off = C('SHOP_IS_OFF');
		if($shop_is_off){
			echo $this->err("休息期间，暂不营业！");
			return ;
		}
		$param = $this->param;
		$shopInfo = $Shop->where(array("id"=>$param['shop_id']))->field("floor_num,min_consumption,is_super,status")->find();
		if($shopInfo['status']!=1){
			echo $this->err("点铺休息中，不能下单！");
			return ;
		}
		//获取地址
		$addressInfo = $Address->where('id = '.$param['addressId'].' and user_id = '.$param['userId'])->field('id,school_id,schoolName,province,city,area,address,floor_num,contact_tel,contact_name')->find();
		if(empty($param['addressId']) || empty($addressInfo)){
			echo $this->err("错误的地址！");
			return ;
		}
		if($addressInfo['school_id']!=0 && $addressInfo['school_id']!=$param['school_id']){
			echo $this->err("不在派送范围内！");
			return ;
		}
		if($addressInfo['school_id']==$param['school_id']){
			if($shopInfo['floor_num']!=$addressInfo['floor_num']){
				echo $this->err("校园直达只允许楼内派送！");
				return ;
			}

		}
		if(empty($param['goods'])){
			echo $this->err("请至少选择一个商品！");
			return ;
		}
		$Order->startTrans();//开启事务
		$code = '10'.date('ymdHis').rand(10, 99);
		//组装商品数据
		$goods = explode(',', $param['goods']);
		$productIds = '';
		foreach ($goods as $k=>$v){
			$v = explode('_', $v);
			$productId = $v[0];
			$productNum = $v[1];
			if(isset($v[2])){
				$productAtt = $v[2];
				$attr_arr = explode(":",$productAtt);
				$p_attr_v = $ShopAttributeValues->where(array('product_id'=>$productId,'flag'=>1,'id'=>$attr_arr[2],'shop_id'=>$param['shop_id']))->find();
				if(!empty($p_attr_v)){
					$proArr[$productId][$attr_arr[2]] = $productNum;
					$p_attr_data[$productId][$attr_arr[2]]['attribute_values'] = $attr_arr[0].":". $attr_arr[1];
					$p_attr_data[$productId][$attr_arr[2]]['sell_num'] = $productNum; //购买数量
					$p_attr_data[$productId][$attr_arr[2]]['num'] = $p_attr_v['num'];
				}else{
					$proArr[$productId] = $productNum;
				}
			}else{
				$proArr[$productId] = $productNum;
			}
			$productIds .= $productId .",";
		}
		$productIds = rtrim($productIds,",");
		$where['flag'] = 1;
		$where['id'] = array('in',$productIds);
		$productList = $Product->where($where)->field('id,photo,name,price,is_on,num,vip_price')->select();
		$rebackProductData = array();
		$totalPrice = 0;
		$vip_discount = 0;
		foreach ($productList as $pk=>$pv){
			$shopProductInfo = $ShopProduct->field("num,shop_price")->where(array('product_id' => $pv['id'], 'flag' => 1,'shop_id'=>$param['shop_id']))->find();
			if($shopProductInfo['shop_price']>0){
				$pv['price'] = $shopProductInfo['shop_price'];
			}
			if(isset($p_attr_data[$pv['id']])){
				foreach($p_attr_data[$pv['id']] as $p_a_v_key => $p_attr_data_v){
					$sellNum = $p_attr_data_v['sell_num'];
					if($sellNum > $p_attr_data_v['num']){
						echo $this->err($pv['name'].$p_attr_data_v['attribute_values'].'库存不足，请重新选择商品吧！');
						return ;
					}
					//商品下架
					if($pv['is_on'] == 2){
						echo $this->err($pv['name'].'已下架，请重新选择商品！');
						return ;
					}
					//计算优惠金额
					if($pv['vip_price']>0){
						$vip_discount = $vip_discount+($pv['price']-$pv['vip_price'])*$sellNum;
					}
					$totalPrice += $pv['price'] * $sellNum;

					$detailsData_v['order_code'] = $code;
					$detailsData_v['product_id'] = $pv['id'];
					$detailsData_v['photo'] = $pv['photo'];
					$detailsData_v['name'] = $pv['name'];
					$detailsData_v['price'] = $pv['price'];
					$detailsData_v['vip_price'] = $pv['vip_price'];
					$detailsData_v['num'] = $sellNum;
					$detailsData_v['create_time'] = time();
					$detailsData_v['attribute_values'] = $p_attr_data_v['attribute_values'];
					$detailsData_v['attr_values_id'] = $p_a_v_key;
					$detailsData[] =$detailsData_v;


					//准备返回客户端数据
					$rebackProductData_v['name'] = $pv['name'];
					$rebackProductData_v['price'] = $pv['price'];
					$rebackProductData_v['num'] = $sellNum;
					$rebackProductData_v['attribute_values'] = $p_attr_data_v['attribute_values'];
					$rebackProductData[] = $rebackProductData_v;
				}
			}else{
				$sellNum = $proArr[$pv['id']];
				if($sellNum > $shopProductInfo['num']){
					echo $this->err($pv['name'].'库存不足，请您重新选择商品！');
					return ;
				}
				//商品下架
				if($pv['is_on'] == 2){
					echo $this->err($pv['name'].'已下架，请重新选择商品！');
					return ;
				}
				$totalPrice += $pv['price'] * $sellNum;
				if(isset($shopInfo['min_consumption']) && $shopInfo['min_consumption']){
					$min_consumption = $shopInfo['min_consumption'];
				}else{
					$min_consumption = 0;
				}
				//计算优惠金额
				if($pv['vip_price']>0){
					$vip_discount = $vip_discount+($pv['price']-$pv['vip_price'])*$sellNum;

				}
				$detailsData_v['order_code'] = $code;
				$detailsData_v['product_id'] = $pv['id'];
				$detailsData_v['photo'] = $pv['photo'];
				$detailsData_v['name'] = $pv['name'];
				$detailsData_v['price'] = $pv['price'];
				$detailsData_v['vip_price'] = $pv['vip_price'];
				$detailsData_v['num'] = $sellNum;
				$detailsData_v['create_time'] = time();
				$detailsData_v['attribute_values'] = "";
				$detailsData_v['attr_values_id'] = 0;
				$detailsData[] =$detailsData_v;


				//准备返回客户端数据
				$rebackProductData_v['name'] = $pv['name'];
				$rebackProductData_v['price'] = $pv['price'];
				$rebackProductData_v['num'] = $sellNum;
				$rebackProductData[] = $rebackProductData_v;
			}
		}

		//组装配送参数
		$data['province'] = $addressInfo['province'];
		$data['city'] = $addressInfo['city'];
		$data['area'] = $addressInfo['area'];
		$data['contact_name'] = $addressInfo['contact_name'];
		$data['contact_tel'] = $addressInfo['contact_tel'];
		if($addressInfo['school_id'] == 0){//快递模式
			$data['mail_type'] = 2;
			$data['address'] = $addressInfo['address'];
			$mailCustomFee = (float) $Fields->where('field = "app_mail_custom_fee" and flag = 1')->getField('value');//快递模式配送费
			$mailFreeFee = (float) $Fields->where('field = "app_mail_free_fee" and flag = 1')->getField('value');//快递模式免配送费达到金额
			if($totalPrice >= $mailFreeFee){
				$mailFee = 0;
			}else{
				$mailFee = $mailCustomFee;
			}
		}else{
			if(empty($param['mailTime'])){
				echo $this->err('请选择配送时间段！');
				return;
			}
			$data['mail_type'] = 1;
			$data['address'] = $addressInfo['schoolName'].$addressInfo['floor_num'].$addressInfo['address'];
			$mailSchoolFee = (float) $Fields->where('field = "app_mail_school_fee" and flag = 1')->getField('value');//校园模式配送费
			$mailSchoolStartFee = (float) $Fields->where('field = "app_mail_school_start_fee" and flag = 1')->getField('value');//校园模式起送费

			//计算优惠金额
			if($min_consumption>0 ){
				if($totalPrice<$min_consumption){
					echo $this->err('最低消费'.$min_consumption.'元！请继续填充您的购物车');
					return ;
				}
			}else{
				if($totalPrice < $mailSchoolStartFee){
					echo $this->err('未达到起送金额：'.$mailSchoolStartFee.'元，再挑一点吧！');
					return;
				}
			}
			$mailFee = $mailSchoolFee;
			$data['mail_time'] = $param['mailTime'];
		}
		$data['code'] = $code;
		$data['user_id'] = $param['userId'];
		$data['register_tel'] = $User->where('id = '.$param['userId'])->getField('tel');
		$data['total_price'] = $totalPrice;
		$data['mail_fee'] = $mailFee;
		$data['real_price'] = $totalPrice;
		$data['status'] = 1;
		$data['create_time'] = time();
		$data['remark'] = $param['remark'];
		$data['shop_id'] = $param['shop_id'];
		//插入数据
		$result1 = $Order->data($data)->add();
		$result2 = $OrderDetails->addAll($detailsData);

		if($result1 && $result2){
			$Order->commit();
			$rebackData['code'] = $code;
			$rebackData['realPrice'] = $totalPrice;
			$rebackData['vip_discount'] = $vip_discount;
			$rebackData['mailFee'] = $mailFee;
			$rebackData['productList'] = $rebackProductData;
			echo $this->suc($rebackData);
		}else{
			$Order->rollback();
			echo $this->err('服务器繁忙！');
		}
	}

	/**
	 * 取消订单
	 */
	public function cancelOrder(){
		$Order = M('Order');
		$param = $this->param;
		if(empty($param['orderCode'])){
			echo $this->err('参数错误！');
			return ;
		}
		$where['flag'] = 1;
		$where['code'] = $param['orderCode'];
		$info = $Order->where($where)->find();
		if(count($info) == 0){
			echo $this->err('未找到该订单！');
			return ;
		}
		if($info['status'] != 1){
			echo $this->err('错误订单状态，无法继续！');
			return ;
		}
		//数量回补
		backProductNum($info['code']);
		$result = $Order->where($where)->setField(array('status'=>-1,'update_time'=>time()));
		if($result !== false){
			echo $this->suc();
		}else{
			echo $this->err('修改失败！');
		}
	}

	/**
	 * 订单列表
	 */
	public function orderList(){
		$Order = M('Order');$OrderDetails = M('OrderDetails');
		$param = $this->param;
		if(is_null($param['nextStartId'])){
			echo $this->err('参数错误！');
			return ;
		}
		$where['flag'] = 1;
		$where['user_id'] = $param['userId'];
		if(!empty($param['status'])){
			$where['status'] = array('in',$param['status']);
		}
		$count = $Order->where($where)->count();
		$field = 'id,code,create_time as createTime,status,real_price as realPrice,mail_fee as mailFee';
		if($count < 5){
			$nextStartId = -1;
			$data = $Order->where($where)->field($field)->order('create_time desc')->select();
		}else{
			$nextStartId = $param['nextStartId'];
			$data = $Order->where($where)->field($field)->limit($nextStartId,5)->order('create_time desc')->select();
			$nextStartId = $nextStartId + 5;
			if($nextStartId >= $count || count($data) == 0){
				$nextStartId = -1;
			}
		}
		//组装产品
		foreach ($data as $key=>$val){
			$productList = $OrderDetails->where('flag = 1 and order_code = "'.$val['code'].'"')->field('id,photo,name,price,num,vip_price,attribute_values')->select();
			//计算每个订单优惠金额
			$vip_discount=0;
			foreach($productList as $p_key=>$p_v){
				if($p_v['vip_price']>0){
					$vip_discount = $vip_discount+($p_v['price']-$p_v['vip_price'])*$p_v['num'];
				}
			}
			$data[$key]['vip_discount'] = $vip_discount;
			$data[$key]['productList'] = $productList;
		}
		echo $this->suc($data,$nextStartId);
	}
	/**
	 * 我的店铺订单列表
	 */
	public function myShopOrderList(){
		$Order = M('Order');$OrderDetails = M('OrderDetails');
		$param = $this->param;
		if(is_null($param['nextStartId'])){
			echo $this->err('参数错误！');
			return ;
		}
		$where['flag'] = 1;

		if(!empty($param['status'])){
			$where['status'] = array('in',$param['status']);
		}
		$shopInfo = M("Shop")->field("id")->where(array("user_id"=>$param['userId']))->find();
		if($shopInfo && $param['userId']!=10015 && $param['userId']!=12750){
			$userInfo = M("User")->field("phone_manage")->where(array("id"=>$param['userId']))->find();
			if($userInfo['phone_manage']==0){
				$where['shop_id'] = $shopInfo['id'];
			}
		}

		$count = $Order->where($where)->count();
		$field = 'id,code,create_time as createTime,status,real_price as realPrice,mail_fee as mailFee';
		if($count < 5){
			$nextStartId = -1;
			$data = $Order->where($where)->field($field)->order('create_time desc')->select();
		}else{
			$nextStartId = $param['nextStartId'];
			$data = $Order->where($where)->field($field)->limit($nextStartId,5)->order('create_time desc')->select();
			$nextStartId = $nextStartId + 5;
			if($nextStartId >= $count || count($data) == 0){
				$nextStartId = -1;
			}
		}
		//组装产品
		foreach ($data as $key=>$val){
			$productList = $OrderDetails->where('flag = 1 and order_code = "'.$val['code'].'"')->field('id,photo,name,price,num,attribute_values')->select();
			$data[$key]['productList'] = $productList;
		}
		echo $this->suc($data,$nextStartId);
	}
	/**
	 * 订单详情
	 */
	public function getOrderInfo(){
		$Order = M('Order');$OrderDetails = M('OrderDetails');
		$param = $this->param;
		if(empty($param['orderCode'])){
			echo $this->err('参数错误！');
			return ;
		}
		$where['flag'] = 1;
		$where['code'] = $param['orderCode'];
		$field = 'id,code,status,create_time as createTime,pay_time as payTime,give_time as giveTime,done_time as doneTime,update_time as updateTime,mail_time as mailTime
				,mail_fee as mailFee,real_price as realPrice,province,city,area,address,contact_name as contactName,contact_tel as contactTel,remark,return_apply_time as returnApplyTime,
				return_reason as returnReason,return_time as returnTime,mail_type as mailType';
		$data = $Order->where($where)->field($field)->find();
		if(count($data) == 0){
			echo $this->err('未找到该订单！');
			return ;
		}
		//获取商品详情
		$productList = $OrderDetails->where('flag = 1 and order_code = '.$param['orderCode'])->field('photo,name,price,num,attribute_values')->select();
		$data['productList'] = $productList;
		echo $this->suc($data);
	}

	/**
	 * 订单详情
	 */
	public function getMyShopOrderInfo(){
		$Order = M('Order');$OrderDetails = M('OrderDetails');
		$param = $this->param;
		if(empty($param['orderCode'])){
			echo $this->err('参数错误！');
			return ;
		}
		$where['flag'] = 1;
		$where['code'] = $param['orderCode'];
		$field = 'id,code,status,create_time as createTime,pay_time as payTime,give_time as giveTime,done_time as doneTime,update_time as updateTime,mail_time as mailTime
				,mail_fee as mailFee,real_price as realPrice,province,city,area,address,contact_name as contactName,contact_tel as contactTel,remark,return_apply_time as returnApplyTime,
				return_reason as returnReason,return_time as returnTime,mail_type as mailType';
		$data = $Order->where($where)->field($field)->find();
		if(count($data) == 0){
			echo $this->err('未找到该订单！');
			return ;
		}
		//获取商品详情
		$productList = $OrderDetails->where('flag = 1 and order_code = '.$param['orderCode'])->field('photo,name,price,num,attribute_values')->select();
		$data['productList'] = $productList;
		echo $this->suc($data);
	}

	/**
	 * 确认订单
	 * 上级获得分层
	 */
	public function doneOrder(){
		$Order = M('Order');$User = M('User');$Fields = M("Fields");$UserDetails = M('UserDetails');$Product = M('Product');$OrderDetails = M('OrderDetails');
		$param = $this->param;
		$Order->startTrans();
		if(empty($param['orderCode'])){
			echo $this->err('参数错误！');
			return ;
		}
		$where['flag'] = 1;
		$where['code'] = $param['orderCode'];
		$info = $Order->where($where)->find();
		if(count($info) == 0){
			echo $this->err('未找到该订单！');
			return ;
		}
		if(!in_array($info['status'], array(3,-3))){
			echo $this->err('错误订单状态，无法继续！');
			return ;
		}
		$parentId = $User->where('id = '.$info['user_id'])->getField('parent_id');
		$result1 = true;$result2 = true;
		//分润
		if(!empty($parentId)){
			$bl = (float) $Fields->where('field = "app_consume_return_rate" and flag = 1')->getField('value');//上级返现利率
			$price = sprintf("%.2f",$info['real_price'] * $bl);
			if(!empty($price)){
				$parentBalance = $User->where('id ='.$parentId)->getField("balance");
				$result1 = $this->addUserDetails($parentId,"消费返利",$price,$info['user_id'],$parentBalance);
				if($price>0){
					$result2 = $User->where('id = '.$parentId)->setInc('balance',$price);
				}
			}
		}
		//已售数量+1
		$map['flag'] = 1;
		$map['order_code'] = $param['orderCode'];
		$udList = $OrderDetails->where($map)->field('product_id,num')->select();
		foreach ($udList as $k=>$v){
			$Product->where('id = '.$v['product_id'])->setInc('sell_num',$v['num']);
		}
		$data['status'] = 4;
		$data['done_time'] = time();
		$result3 = $Order->where($where)->save($data);
		if($result1 && $result2 && $result3){
			$Order->commit();
			echo $this->suc();
		}else{
			$Order->rollback();
			echo $this->err('修改失败！');
		}
	}

	/**
	 * 记录金额明细
	 */
	private function addUserDetails($userId,$title,$price,$formUserId,$balance){
		$UserDetails = M('UserDetails');
		$detailData['user_id'] = $userId;
		$detailData['title'] = $title;
		$detailData['type'] = 1;
		$detailData['price'] = $price;
		$detailData['from_user_id'] = $formUserId;
		$detailData['last_price'] = $balance;
		$detailData['create_time'] = time();
		$result = $UserDetails->add($detailData);
		if($result){
			return true;
		}else{
			return false;
		}
	}

	/**
	 * 申请退款
	 */
	public function applyReturnOrder(){
		$Order = M('Order');$Shop = M("Shop");$OrderDetails = M('OrderDetails');$Product = M('Product');$ShopProduct = M("ShopProduct");$ShopAttributeValues = M("ShopAttributeValues");
		$param = $this->param;
		if(empty($param['orderCode'])){
			echo $this->err('参数错误！');
			return ;
		}
		$where['flag'] = 1;
		$where['code'] = $param['orderCode'];
		$info = $Order->where($where)->find();
		if(count($info) == 0){
			echo $this->err('未找到该订单！');
			return ;
		}
		if(!in_array($info['status'], array(2,3))){
			echo $this->err('错误订单状态，无法继续！');
			return ;
		}

		//扣除库存
		$orderDetails = $OrderDetails->where(array('order_code'=>$info['code'],'flag'=>1))->select();
		$querySql = 'SET num = CASE id ';
		$queryIds = "";

		$querySql_s = 'SET num = CASE product_id ';
		$queryIds_s = "";

		$querySql_1 = 'SET num = CASE id ';
		$queryIds_1 = "";

		foreach ($orderDetails as $pk => $pv) {
			$sellNum = $pv['num'];
			//组装扣除商品数量sql
			if ($pv['attribute_values'] != "") {
				$querySql_1 .= ' WHEN ' . $pv['attr_values_id'] . ' THEN num +' . $sellNum . " ";
				$queryIds_1 .= $pv['attr_values_id'] . ",";
			} else {
				$querySql .= ' WHEN ' . $pv['product_id'] . ' THEN num +' . $sellNum . " ";
				$queryIds .= $pv['product_id'] . ",";
				$querySql_s .= ' WHEN ' . $pv['product_id'] . ' THEN num +' . $sellNum . " ";
				$queryIds_s .= $pv['product_id'] . ",";
			}
		}

		if ($queryIds_1 != '') {
			$querySql_1 .= ' END ';
			$queryIds_1 = substr($queryIds_1, 0, strlen($queryIds_1) - 1);
			$querySql_1 .= 'where id in (' . $queryIds_1 . ') and shop_id='.$info['shop_id'];
		}
		if ($queryIds != '') {
			$querySql .= ' END ';
			$queryIds = substr($queryIds, 0, strlen($queryIds) - 1);
			$querySql .= 'where id in (' . $queryIds . ')';
		}

		if ($queryIds_s != '') {
			$querySql_s .= ' END ';
			$queryIds_s = substr($queryIds_s, 0, strlen($queryIds_s) - 1);
			$querySql_s .= 'where product_id in (' . $queryIds_s . ') and shop_id=' . $info['shop_id'];
		}

		//扣除商品表库存
		if ($queryIds != '') {
			$Product->execute('update __TABLE__ ' . $querySql);
		}
		//扣除店铺规格库存
		if ($queryIds_1 != '') {
			$ShopAttributeValues->execute('update __TABLE__ ' . $querySql_1);
		}
		//扣除店铺商品库存
		if ($queryIds_s != '') {
			$ShopProduct->execute('update __TABLE__ ' . $querySql_s);
		}
		$data['status'] = 0;
		$data['return_apply_time'] = time();
		$data['return_reason'] = $param['returnReason'];
		$result = $Order->where($where)->save($data);
		if($result !== false){
			echo $this->suc();
		}else{
			echo $this->err('修改失败！');
		}
	}

	/**
	 * 配送
	 */
	public function deliveryOrder(){
		$Order = M('Order');
		$param = $this->param;
		if(empty($param['orderCode'])){
			echo $this->err('参数错误！');
			return ;
		}
		$where['flag'] = 1;
		$where['code'] = $param['orderCode'];
		$info = $Order->where($where)->find();
		if(count($info) == 0){
			echo $this->err('未找到该订单！');
			return ;
		}
		if($info['status']!=2){
			echo $this->err('错误订单状态，无法继续！');
			return ;
		}
		$data['status'] = 3;

		$data['give_time'] = time();
		$result = $Order->where($where)->save($data);
		if($result !== false){
			$this->insertNotice($info['user_id'], "您的订单已发货！", $info['code']);
			echo $this->suc();
		}else{
			echo $this->err('修改失败！');
		}
	}

	/**
	 * 新增消息
	 * 只新增个人订单消息
	 */
	private function insertNotice($userId,$message,$ambulance){
		$Notice = M('Notice');$User = M('User');
		$jpushId = $User->where('id = '.$userId)->getField('jpush_id');
		if(!empty($jpushId)){
			$data['type'] = 1;
			$data['user_id'] = $userId;
			$data['message'] = $message;
			$data['ambulance'] = $ambulance;
			$data['create_time'] = time();
			$Notice->add($data);
		}
		return true;
	}
}